﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using System.Diagnostics;
using System.Configuration;
using System.Windows.Forms;
using System.Data;
using System.Collections;

namespace SuraQR.Controller
{
    class OracleDriver11g
    {
        
        private OracleConnection conn;
        private OracleCommand cmd;

        public void init()
        {
            String url = ConfigurationManager.AppSettings["dbUrl"];

            try
            {
                conn = new OracleConnection(url);  // C#
                conn.Open();
                Debug.WriteLine("Connect success!!!");
                cmd = new OracleCommand();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                MessageBox.Show("Error connect Oracle DB \n" + e.ToString());
            }
          
        }

       
        public int updateCmd(string sql)
        {

            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            return cmd.ExecuteNonQuery();
        }


        public void transectionUpdate(ArrayList sqls)
        {
            //updateCmd("begin");
            try
            {
                foreach (String s in sqls)
                {
                    updateCmd(s);
                }

                updateCmd("commit");
                Debug.WriteLine("COMMIT !!");
            }
            catch (Exception e)
            {
                Debug.WriteLine("Rollblack !!");
                Debug.WriteLine(e);
                updateCmd("rollback");
                throw new Exception("rollback");
                
            }

        }

        public void close()
        {
            conn.Close();
        }
    }
}
